﻿#include "WhiteMammoth.h"
#include "../../../Shared/Functions/Functions.h"
#include "../../../Shared/ServerPackets.h"
#include "../../../Shared/Data/Stat.h"
#include "../DelayedAction.h"
#include "../../../Shared/Enums.h"
#include "../MapObject.h"

using namespace Server::MirDatabase;
namespace S = ServerPackets;

namespace Server::MirObjects::Monsters
{

    WhiteMammoth::WhiteMammoth(MonsterInfo *info) : MonsterObject(info)
    {
    }

    void WhiteMammoth::Attack()
    {
        if (!getTarget()->IsAttackTarget(this))
        {
            setTarget(nullptr);
            return;
        }

        setDirection(Functions::DirectionFromPoint(getCurrentLocation(), getTarget()->getCurrentLocation()));

        if (getEnvir()->Random->Next(8) > 0)
        {
            if (getEnvir()->Random->Next(6) > 0)
            {
                MonsterObject::Attack();
            }
            else
            {
                S::ObjectAttack *tempVar = new S::ObjectAttack();
                tempVar->ObjectID = ObjectID;
                tempVar->Direction = getDirection();
                tempVar->Location = getCurrentLocation();
                tempVar->Type = 1;
                Broadcast(tempVar);
                int damage = GetAttackPower(Stats[Stat::MinDC], Stats[Stat::MaxDC] * 2);
                if (damage == 0)
                {
//C# TO C++ CONVERTER TODO TASK: A 'delete tempVar' statement was not added since tempVar was passed to a method or constructor. Handle memory management manually.
                    return;
                }

                DelayedAction *action = new DelayedAction(DelayedType::Damage, getEnvir()->getTime() + 300, {getTarget(), damage, DefenceType::ACAgility, false});
                ActionList.push_back(action);

//C# TO C++ CONVERTER TODO TASK: A 'delete action' statement was not added since action was passed to a method or constructor. Handle memory management manually.
//C# TO C++ CONVERTER TODO TASK: A 'delete tempVar' statement was not added since tempVar was passed to a method or constructor. Handle memory management manually.
            }
        }
        else
        {
            S::ObjectAttack *tempVar2 = new S::ObjectAttack();
            tempVar2->ObjectID = ObjectID;
            tempVar2->Direction = getDirection();
            tempVar2->Location = getCurrentLocation();
            tempVar2->Type = 2;
            Broadcast(tempVar2);

            int damage = GetAttackPower(Stats[Stat::MinDC], Stats[Stat::MaxDC]);
            if (damage == 0)
            {
//C# TO C++ CONVERTER TODO TASK: A 'delete tempVar2' statement was not added since tempVar2 was passed to a method or constructor. Handle memory management manually.
                return;
            }

            DelayedAction *action = new DelayedAction(DelayedType::Damage, getEnvir()->getTime() + 300, {getTarget(), damage, DefenceType::ACAgility, true});
            ActionList.push_back(action);

//C# TO C++ CONVERTER TODO TASK: A 'delete action' statement was not added since action was passed to a method or constructor. Handle memory management manually.
//C# TO C++ CONVERTER TODO TASK: A 'delete tempVar2' statement was not added since tempVar2 was passed to a method or constructor. Handle memory management manually.
        }

        ShockTime = 0;
        ActionTime = getEnvir()->getTime() + 300;
        AttackTime = getEnvir()->getTime() + AttackSpeed;

    }

    void WhiteMammoth::CompleteAttack(std::vector<std::any> &data)
    {
        int damage = std::any_cast<int>(data[1]);

        if (data.size() >= 4)
        {
            bool stomp = std::any_cast<bool>(data[3]);

            if (stomp)
            {
                std::vector<MapObject*> targets = FindAllTargets(1, getCurrentLocation(), false);

                for (int i = 0; i < targets.size(); i++)
                {
                    auto target = targets[i];

                    if (target == nullptr || !target->IsAttackTarget(this) || target->getCurrentMap() != getCurrentMap() || target->Node == nullptr)
                    {
                        continue;
                    }

                    if (target->Attacked(this, damage) <= 0)
                    {
                        continue;
                    }

                    PoisonTarget(target, 0, 5, PoisonType::Dazed, 2000);
                }

                return;
            }
        }

        MonsterObject::CompleteAttack(data);
    }
}
